import type { Context } from 'koa'
import { Post, Get } from '../../router'

import { database } from '../../database'

import { Authorize } from '../../router/authorize'

import { User } from '../db'
import { result } from '../index'

export default class UserInfo {
  @Get('/user/:id/get')
  @Authorize()
  static async getUserInfoById(ctx: Context) {
    const { id } = ctx.params
    const user = ctx.state.user
    console.log('user  ', user)
    if (!id || user.id !== id) {
      return ctx.throw(400, '非法用户')
    }
    try {
      const db = database.getRepository(User)
      const data = await db.findOneBy({
        id
      })
      ctx.body = result({ data })
    } catch (error) {
      return ctx.throw(500, error as string)
    }
  }

  @Post('/update/:id')
  @Authorize()
  static async updateUser(ctx: Context) {
    const { id } = ctx.params
    const user = ctx.state.user
    const body = ctx.request.body
    console.log('user  ', user)
    if (!id || user.id !== id) {
      return ctx.throw(400, '非法用户')
    }
    try {
      const db = database.getRepository(User)
      const userInfo = await db.findOneBy({ id })
      const res = await db.update(id, Object.assign({}, userInfo, body))
      if (res && res.affected) {
        ctx.body = result({
          message: '修改成功'
        })
        return
      }
      ctx.throw(400, '修改失败')
    } catch (error) {
      return ctx.throw(500, error)
    }
  }

  @Get('/user')
  static async getUserByUsername(ctx: Context) {
    try {
      const { pagesize = 10, pageindex = 0 } = ctx.query
      const take = isNaN(Number(pagesize)) ? 10 : Number(pagesize)
      const skip = isNaN(Number(pageindex)) ? 0 : Number(pageindex)
      const db = database.getRepository(User)
      const user = await db.findAndCount({
        where: {
          // age: 16
        },
        // select: ['id', 'nickname', 'url'],
        /**
         * take => 每页显示数量
         * skip => 翻页
         */
        take,
        skip: take * skip
        // relations: {
        //   login: true
        // }
      })
      if (!user.length)
        return (ctx.body = result({
          message: '用户不存在'
        }))
      return (ctx.body = result({
        data: {
          user: user[0],
          count: user[1]
        }
      }))
    } catch (error) {
      ctx.throw(500, error as string)
    }
  }
  // @Get('/user/:id')
  // static async getUserByUsername(ctx: Context) {
  //   const { id } = ctx.params
  //   if (!id) return ctx.throw(400, '参数错误')
  //   try {
  //     const db = database.getRepository(User)
  //     const user = await db.findOne({
  //       where: {
  //         id
  //       },
  //       select: ['id', 'nickname', 'url'],
  //       relations: {
  //         login: true
  //       }
  //     })
  //     // const user = await db
  //     //   .createQueryBuilder('user')
  //     //   .innerJoinAndSelect('user.login', 'login')
  //     //   // .where('user.id = :id')
  //     //   // .setParameters({ id })
  //     //   .take(100)
  //     //   // .select(['login.username', 'user.nickname', 'user.id'])
  //     //   .getMany()
  //     // // const user = await database.getRepository(User).find()
  //     // console.log(user)
  //     if (!user)
  //       return (ctx.body = result({
  //         message: '用户不存在'
  //       }))
  //     return (ctx.body = result({
  //       data: user
  //     }))
  //   } catch (error) {
  //     ctx.throw(500, error as string)
  //   }
  // }
}
